<template>
  <div class="cesium-layer-widgets" :style="{ right: threeWidth + 40 + 'px' }">
    <div v-for="item in LayerOptions" :key="item.id" @click="handleChecked(item)">
      <img :src="item.img" alt="" />
      <span>{{ item.name }}</span>
    </div>
    <el-checkbox v-model="checked" @change="changeCheck" style="color: #fff; font-size: 16px">加载地形</el-checkbox>
    <!-- <div class="layer-widgets-header">
      <div><i class="dx-layer" /><span>业务图层</span></div>
      <div><i class="el-icon-close" @click="closeWidget" /></div>
    </div>
    <div class="layer-widgets-body">
      <el-collapse v-model="activeName" accordion>
        <el-collapse-item
          :title="item.name"
          :name="item.collapseIndex"
          v-for="item in LayerOptions"
          :key="item.id"
        >
          <div>
            <el-checkbox-group
              v-model="checkedCities"
              @change="handleCheckedCitiesChange"
            >
              <el-checkbox
                v-for="imgItem in item.imagProviders"
                :label="imgItem.name"
                :key="imgItem.id"
                >{{ imgItem.name }}</el-checkbox
              >
            </el-checkbox-group>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div> -->
  </div>
</template>

<script>
import LayerOptions from "./layerOptions";
import { mapState } from "vuex";

export default {
  name: "CesiumLayerWidgets",
  computed: {
    ...mapState({
      threeWidth: (state) => state.app.threeWidth, // three模型全屏展示
    }),
  },
  data() {
    return {
      LayerOptions: LayerOptions,
      checkAll: false,
      checkedCities: [],
      isIndeterminate: true,
      activeName: "1",
      checked: false,
      checked2: false,
      checked5: false,
      checked6: true,
    };
  },
  methods: {
    closeWidget() {
      this.$bus.emit("cesiumBusEvent", "layerWidgets");
    },
    changeCheck() {
      if (this.checked) {
        var token = "4676cbcd5a7b93b0e7f4ab6f6a4f8fec";
        // 服务域名
        var tdtUrl = "https://t{s}.tianditu.gov.cn/";
        // 服务负载子域
        var subdomains = ["0", "1", "2", "3", "4", "5", "6", "7"];

        var terrainUrls = new Array();

        for (var i = 0; i < subdomains.length; i++) {
          var url =
            tdtUrl.replace("{s}", subdomains[i]) +
            "mapservice/swdx?tk=" +
            token;
          terrainUrls.push(url);
        }

        // var provider = new Cesium.GeoTerrainProvider({
        //   urls: terrainUrls,
        // });

        var provider = new Cesium.CesiumTerrainProvider({
          //地质开挖必须是cesium提供的地形
          // url: 'https://lab.earthsdk.com/terrain/4676cbcd5a7b93b0e7f4ab6f6a4f8fec', //  Cesium.IonResource.fromAssetId(1),
          url: "https://lab.earthsdk.com/terrain/577fd5b0ac1f11e99dbd8fd044883638", //  Cesium.IonResource.fromAssetId(1),
          requestVertexNormals: true, //顶点法线效果
          requestWaterMask: true, //水效果
        });
        viewer.terrainProvider = provider;
      } else {
        viewer.scene.terrainProvider = new Cesium.EllipsoidTerrainProvider({});
      }
    },
    handleChecked(data) {
      if (data.imageryProvider) {
        viewer.imageryLayers.removeAll();
        if (
          Object.prototype.toString.call(data.imageryProvider) ===
          "[object Array]"
        ) {
          for (let i = 0; i < data.imageryProvider.length; i++) {
            const layer = viewer.imageryLayers.addImageryProvider(
              data.imageryProvider[i]
            );
          }
        } else {
          viewer.imageryLayers.addImageryProvider(data.imageryProvider);
        }
      }
    },
    handleCheckedCitiesChange(value) {
      var arr = [];
      LayerOptions.forEach((item) => {
        if (item && item.imagProviders && item.imagProviders.length) {
          if (value.length) {
            value.forEach((name) => {
              var obj =
                item.imagProviders.find((imgPro) => imgPro.name === name) ||
                null;
              if (obj && obj.imageryProvider) {
                arr.push(obj.imageryProvider);
              }
            });
          }
        }
      });
      if (arr.length) {
        viewer.imageryLayers.removeAll();
        arr.forEach((img) => {
          viewer.imageryLayers.addImageryProvider(img);
        });
      } else {
        viewer.imageryLayers.removeAll();
        viewer.imageryLayers.addImageryProvider(
          new this.Cesium.UrlTemplateImageryProvider({
            url: "https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}",
          })
        );
      }
    },
  },
};
</script>

<style  lang="scss" scoped>
.cesium-layer-widgets {
  width: 160px !important;
  height: 160px !important;
  position: fixed;
  right: 40px;
  bottom: 90px !important;
  z-index: 100000;
  background-color: rgba(0, 0, 0, 0.618);
  box-sizing: border-box;
  box-shadow: 0 0 4px #fff, 0 0 4px #fff inset;
  border-radius: 8px;
  padding: 6px;
  color: #fff;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  font-size: 14px;
  & > div {
    width: 46%;
    height: 46%;
    margin: 2%;
    box-sizing: border-box;
    cursor: pointer;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 8px;
    padding: 2px;
    overflow: hidden;
    border: 2px solid #3f4854;
    & > img {
      width: 100%;
      height: 100%;
      border-radius: 8px;
    }
    & > span {
      position: absolute;
      color: #fff;
      font-size: 12px;
    }
    &:hover {
      box-shadow: 0 0 4px #fff, 0 0 4px #fff inset;
      // background: #48b;
      border-color: #aef;
    }
  }
  // .layer-widgets-header {
  //   width: 100%;
  //   height: 40px;
  //   min-height: 40px;
  //   // border-bottom: 1px solid #ccc;
  //   display: flex;
  //   justify-content: space-between;
  //   align-items: center;
  //   & > div > span {
  //     margin-left: 10px;
  //     letter-spacing: 2px;
  //   }
  //   .el-icon-close {
  //     transition: all 0.6s;
  //     transform: scale(1);
  //     cursor: pointer;
  //   }
  //   .el-icon-close:hover {
  //     transform: scale(2);
  //   }
  // }
  // .layer-widgets-body {
  //   flex: 1;
  //   width: 100%;
  //   padding: 0 8px;
  //   box-sizing: border-box;
  //   overflow: hidden;
  //   overflow-y: auto;
  // }
  // .layer-widgets-footer {
  //   width: 100%;
  //   height: 60px;
  //   min-height: 60px;
  //   border-top: 1px solid #ccc;
  //   display: flex;
  //   justify-content: flex-start;
  //   align-items: center;
  //   .footer-title {
  //     width: 32px;
  //     font-size: 12px;
  //   }
  // }
}
</style>
<style>
/* .cesium-layer-widgets .el-collapse-item__header {
  background-color: transparent !important;
  color: #fff;
}
.cesium-layer-widgets .el-collapse-item__wrap {
  background-color: transparent !important;
  color: #fff;
}
.cesium-layer-widgets .el-checkbox {
  background-color: transparent !important;
  color: #fff;
}
.cesium-layer-widgets .el-checkbox-group {
  display: flex;
  flex-direction: column;
  padding: 0 15px;
} */
</style>